package cn.falconnect.adserver.adapter.persistence.dao;

import java.util.List;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Component;

import cn.falconnect.adserver.domain.ad.AdMaterial;
import cn.falconnect.adserver.domain.ad.AuditPassedMarker;

@Component
public interface AdMaterialDao {

	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "creative_id", property = "creativeId"),
			@Result(column = "material_type_id", property = "materialTypeId"),
			@Result(column = "material_format", property = "materialFormat"),
			@Result(column = "material_url", property = "materialUrl"),
			@Result(column = "material_text", property = "materialText"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "size_id", property = "adSize",one=@One(select="cn.falconnect.adserver.adapter.persistence.dao.AdSizeDao.findValidById",fetchType = FetchType.DEFAULT)),
			//@Result(column = "id",property ="platformIds",many = @Many(select = "findMaterialAuditPlatform" ,fetchType = FetchType.DEFAULT)),
			@Result(column = "id",property ="AuditPassedMarkers",many = @Many(select = "findMaterialAuditPlatform" ,fetchType = FetchType.DEFAULT))
	})
	@Select("select * from ad_material")
	List<AdMaterial> findAll();
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "platform_id", property = "platformId"),
			@Result(column = "audit_id", property = "auditId")
	})
	@Select("select id,platform_id,audit_id from ad_material_audit where material_id = #{id} and audit_status=2 and status=1")
	List<AuditPassedMarker> findMaterialAuditPlatform(int id);

	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "creative_id", property = "creativeId"),
			@Result(column = "material_type_id", property = "materialTypeId"),
			@Result(column = "material_format", property = "materialFormat"),
			@Result(column = "material_url", property = "materialUrl"),
			@Result(column = "material_text", property = "materialText"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "size_id", property = "adSize",one=@One(select="cn.falconnect.adserver.adapter.persistence.dao.AdSizeDao.findValidById",fetchType = FetchType.DEFAULT)),
			@Result(column = "id",property ="AuditPassedMarkers",many = @Many(select = "findMaterialAuditPlatform" ,fetchType = FetchType.DEFAULT)),
			/*@Result(column = "id",property ="platformIds",many = @Many(select = "findMaterialAuditPlatform" ,fetchType = FetchType.DEFAULT))*/
	})
	@Select("select * from ad_material where update_time >= #{value}")
	List<AdMaterial> findAfterTime(String warmUpTime);

	@Select("select id from ad_material where creative_id = #{value}")
	List<Integer> findIds(long craetiveId);

	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "creative_id", property = "creativeId"),
			@Result(column = "material_type_id", property = "materialTypeId"),
			@Result(column = "material_format", property = "materialFormat"),
			@Result(column = "material_url", property = "materialUrl"),
			@Result(column = "material_text", property = "materialText"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "size_id", property = "adSize.id"),
			@Result(column = "width", property = "adSize.width"),
			@Result(column = "height", property = "adSize.height"),
			@Result(column = "s_create_time", property = "adSize.createTime"),
			@Result(column = "s_update_time", property = "adSize.updateTime"),
			@Result(column = "s_status", property = "adSize.status")
	})
	@Select("select m.*, s.*, "
			+ "s.status as s_status, s.create_time as s_create_time, s.update_time as s_update_time "
			+ "from ad_material as m left join ad_size as s on m.size_id = s.id where creative_id = #{value}")
	List<AdMaterial> findByCreativeId(long creativeId);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "creative_id", property = "creativeId"),
			@Result(column = "material_type_id", property = "materialTypeId"),
			@Result(column = "material_format", property = "materialFormat"),
			@Result(column = "material_url", property = "materialUrl"),
			@Result(column = "material_text", property = "materialText"),
			@Result(column = "size_id", property = "adSize.id"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime") })
	@Select("select * from ad_material where id = #{value}")
	AdMaterial findById(long id);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "creative_id", property = "creativeId"),
			@Result(column = "material_type_id", property = "materialTypeId"),
			@Result(column = "material_format", property = "materialFormat"),
			@Result(column = "material_url", property = "materialUrl"),
			@Result(column = "material_text", property = "materialText"),
			@Result(column = "size_id", property = "adSize.id"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "id",property ="AuditPassedMarkers",many = @Many(select = "findMaterialAuditPlatform" ,fetchType = FetchType.DEFAULT)),
	})
	@Select("select * from ad_material where id = #{id} and status = #{status}")
	AdMaterial selectById(@Param("id")long id, @Param("status")int status);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "creative_id", property = "creativeId"),
			@Result(column = "material_type_id", property = "materialTypeId"),
			@Result(column = "material_format", property = "materialFormat"),
			@Result(column = "material_url", property = "materialUrl"),
			@Result(column = "material_text", property = "materialText"),
			@Result(column = "size_id", property = "adSize.id"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime")
	})
	@Select("select * from ad_material where creative_id = #{creativeId}")
	List<AdMaterial> findAllByCreativeId(int creativeId);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "creative_id", property = "creative.id"),
			@Result(column = "creative_id", property = "creativeId"),
			@Result(column = "material_type_id", property = "materialTypeId"),
			@Result(column = "material_format", property = "materialFormat"),
			@Result(column = "material_url", property = "materialUrl"),
			@Result(column = "material_text", property = "materialText"),
			@Result(column = "size_id", property = "adSize.id"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "id",property ="AuditPassedMarkers",many = @Many(select = "findMaterialAuditPlatform" ,fetchType = FetchType.DEFAULT)),
	})
	@Select("select * from ad_material where creative_id = #{id} and status = #{status}")
	List<AdMaterial> selectAllByCreativeId(@Param("id")long id, @Param("status")int status);

}
